package com.wangzhixuan.controller.admin;

import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.result.Result;
import com.wangzhixuan.commons.result.Tree;
import com.wangzhixuan.model.Organization;
import com.wangzhixuan.service.IOrganizationService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @description：部门管理
 * @author：zhixuan.wang
 * @date：2015/10/1 14:51
 */
@Api(tags = "5. 部门管理")
@RestController
@RequestMapping("admin/organization")
public class OrganizationController extends BaseController {

    @Autowired
    private IOrganizationService organizationService;


    /**
     * 部门资源树
     *
     * @return
     */
    @PostMapping(value = "/tree")
    public Result<List<Tree>> tree() {
        return renderObj(organizationService.selectTree());
    }

    /**
     * 部门列表
     *
     * @return
     */
    @GetMapping("/treeGrid")
    public Result<List<Organization>> treeGrid() {
        return renderObj(organizationService.selectTreeGrid());
    }

    /**
     * 添加部门
     *
     * @param organization
     * @return
     */
    @PutMapping("/add")
    public Result<String> add(@Valid Organization organization) {
        organization.setCreateTime(new Date());
        organizationService.save(organization);
        return renderSuccess("添加成功！");
    }

    /**
     * 编辑部门
     *
     * @param organization
     * @return
     */
    @PostMapping("/edit")
    public Result<String> edit(@Valid Organization organization) {
        organizationService.updateById(organization);
        return renderSuccess("编辑成功！");
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result<String> delete(Long id) {
        organizationService.removeById(id);
        return renderSuccess("删除成功！");
    }
}